page-data-props.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
  2. import type { IPage } from '@growi/core';
  3. import { getIdStringForRef } from '@growi/core';
  4. import type { model } from 'mongoose';
  5. import type { CrowiRequest } from '~/interfaces/crowi-request';
  6. import type { IShareLink } from '~/interfaces/share-link';
  7. import type { PageModel } from '~/server/models/page';
  8. import type { ShareLinkModel } from '~/server/models/share-link';
  9. import { findPageAndMetaDataByViewer } from '~/server/service/page/find-page-and-meta-data-by-viewer';
  10. import type { ShareLinkPageStatesProps } from './types';
  11. let mongooseModel: typeof model;
  12. let Page: PageModel;
  13. let ShareLink: ShareLinkModel;
  14. const notFoundProps: GetServerSidePropsResult<ShareLinkPageStatesProps> = {
  15. props: {
  16. isNotFound: true,
  17. pageWithMeta: {
  18. data: null,
  19. meta: {
  20. isNotFound: true,
  21. isForbidden: false,
  22. },
  23. },
  24. isExpired: undefined,
  25. shareLink: undefined,
  26. },
  27. };
  28. export const getPageDataForInitial = async (
  29. context: GetServerSidePropsContext,
  30. ): Promise<GetServerSidePropsResult<ShareLinkPageStatesProps>> => {
  31. const req = context.req as CrowiRequest;
  32. const { crowi, params } = req;
  33. const { pageService, pageGrantService, configManager } = crowi;
  34. if (mongooseModel == null) {
  35. mongooseModel = (await import('mongoose')).model;
  36. }
  37. if (Page == null) {
  38. Page = mongooseModel<IPage, PageModel>('Page');
  39. }
  40. if (ShareLink == null) {
  41. ShareLink = mongooseModel<IShareLink, ShareLinkModel>('ShareLink');
  42. }
  43. const shareLink = await ShareLink.findOne({ _id: params.linkId }).populate(
  44. 'relatedPage',
  45. );
  46. // not found
  47. if (shareLink == null) {
  48. return notFoundProps;
  49. }
  50. const pageId = getIdStringForRef(shareLink.relatedPage);
  51. const pageWithMeta = await findPageAndMetaDataByViewer(
  52. pageService,
  53. pageGrantService,
  54. { pageId, path: null, isSharedPage: true },
  55. );
  56. // not found
  57. if (pageWithMeta.data == null) {
  58. return notFoundProps;
  59. }
  60. // expired
  61. if (shareLink.isExpired()) {
  62. const populatedPage =
  63. await pageWithMeta.data.populateDataToShowRevision(true); //shouldExcludeBody = false,
  64. return {
  65. props: {
  66. isNotFound: false,
  67. pageWithMeta: {
  68. data: populatedPage,
  69. meta: pageWithMeta.meta,
  70. },
  71. isExpired: true,
  72. shareLink: shareLink.toObject(),
  73. },
  74. };
  75. }
  76. // Handle existing page
  77. const ssrMaxRevisionBodyLength = configManager.getConfig(
  78. 'app:ssrMaxRevisionBodyLength',
  79. );
  80. // Check if SSR should be skipped
  81. const latestRevisionBodyLength =
  82. await pageWithMeta.data.getLatestRevisionBodyLength();
  83. const skipSSR =
  84. latestRevisionBodyLength != null &&
  85. ssrMaxRevisionBodyLength < latestRevisionBodyLength;
  86. // Populate page data for display
  87. const populatedPage =
  88. await pageWithMeta.data.populateDataToShowRevision(skipSSR);
  89. return {
  90. props: {
  91. isNotFound: false,
  92. pageWithMeta: {
  93. data: populatedPage,
  94. meta: pageWithMeta.meta,
  95. },
  96. skipSSR,
  97. isExpired: false,
  98. shareLink: shareLink.toObject(),
  99. },
  100. };
  101. };